﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Xin478.CSWork.Common;

namespace Xin478.CSWork.Client
{
    public partial class BoxForm : MyForm
    {
        readonly static object lockHelper = new object();

        int maxDisplay = 5;
        int itemHeight = 20;

        static BoxForm instance =null;

        public int MaxDisplayNum { get { return maxDisplay; } set { maxDisplay = value; } }

        public BoxForm()
        {
            InitializeComponent();
            this.Closing += new CancelEventHandler(BoxForm_Closing);
            this.Load += new EventHandler(BoxForm_Load);
        }

        void BoxForm_Closing(object sender, CancelEventArgs e)
        {
            lock (lockHelper)
            {
                TipList.Instance.ItemRemoved -= new EventHandler(TipList_ItemRemoved);
                instance = null;
            }
        }

        void BoxForm_Load(object sender, EventArgs e)
        {
            this.StartPosition = FormStartPosition.Manual;
            TipList.Instance.ItemRemoved += new EventHandler(TipList_ItemRemoved);
        }

        void TipList_ItemRemoved(object sender, EventArgs e)
        {
            ITipable iTip = sender as ITipable;
            WinForm.DoThreadSafe(this.panel2, delegate()
            {
                foreach (Control ctrl in this.panel2.Controls)
                {
                    MyItem mi = ctrl as MyItem;
                    if (string.Compare(iTip.Key, mi.Tag.ToString()) == 0)
                    {
                        mi.Close();
                        break;
                    }
                }
            });
        }

        public static void Display()
        {
            lock (lockHelper)
            {
                if (instance!= null) return;
            }

            instance = new BoxForm(); 
            int max = Math.Min(TipList.Instance.Count, instance.maxDisplay);
            if (max > 0)
                instance.Height = 42 + max * instance.itemHeight;

            for(int i=0;i<max;i++)
            {
                ITipable iTip = TipList.Instance[i] as ITipable;
                instance.AddItem(iTip);
            }
            instance.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - instance.Width, Screen.PrimaryScreen.WorkingArea.Height - instance.Height);
            instance.Show();
        }

        void AddItem(ITipable iTip)
        {
            MyItem item = new MyItem();
            item.BackHoverColor = System.Drawing.Color.FromArgb(228, 236, 242);
            item.BorderColor = System.Drawing.Color.FromArgb(188, 206, 232);
            item.Dock = System.Windows.Forms.DockStyle.Top;
            item.Size = new System.Drawing.Size(240, instance.itemHeight);
            item.Text = iTip.Title;
            item.Tag = iTip.Key;
            item.Margin = new Padding(0);
            item.Click += new EventHandler(delegate(object o, EventArgs e) { 
                iTip.ItemClick(iTip);
                item.Close();
            });
            item.Closed += delegate(object sender, EventArgs e)
            {
                this.panel2.Controls.Remove(item);
                TipList.Instance.Remove(item.Tag.ToString());
                if (TipList.Instance.Count == 0)
                {
                    this.Close();
                    this.Dispose();
                }
                else
                {
                    if (TipList.Instance.Count > this.panel2.Controls.Count)
                    {
                        AddItem(TipList.Instance[this.panel2.Controls.Count]);
                    }
                    else
                    {
                        this.Height -= this.itemHeight;
                    }
                    this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - instance.Width, Screen.PrimaryScreen.WorkingArea.Height - instance.Height);
                }
            };
            this.panel2.Controls.Add(item);
        }
    }
}
